package com.ce.adapter;

import java.lang.reflect.Method;
import java.util.List;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.ce.activity.R;
import com.ce.activity.SoftApplication;
import com.ce.utils.AppUtils;
import com.ce.utils.AsyncImageLoader;

public class GalleryClassAdapter<T> extends BaseAdapter {
	protected final static int mResDef = R.drawable.pic_dish_default;
	
	protected Context mContext;
	
	protected String mImgFile;
	
	protected int mCount;
	
	protected String mImgPath;
	
	protected View mView;
	
	protected List<T> mDatas;
	
	protected Class<T> mCls;
	
	protected AsyncImageLoader imageLoader;
	
	public GalleryClassAdapter(Context context
						, View view
						, Class<T> cls
						, List<T> datas
						, String imgFile 
						, String imgPath){
		this.mView = view;
		this.mImgFile = imgFile;
		this.mContext = context;
		this.mImgPath = imgPath;
		this.mDatas = datas;
		this.mCls = cls;
		this.mCount = mDatas == null ? 0 : mDatas.size();
		this.mCount = this.mCount > 4 ? 4 : this.mCount; 
		this.imageLoader = ((SoftApplication)mContext).getImageLoader();
	}
	
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}
	
	public int getArrayAcount() {
		return this.mCount;
	}

	@Override
	public T getItem(int position) {
		position = position % mCount;
		return mDatas.get(position); 
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	protected Object getFileValue(T obj) {
		String fieldName = mImgPath;
		String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		try{
			Method method = mCls.getDeclaredMethod(methodName, new Class[]{});
			return method.invoke(obj, new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mCount == 0)
			return null;
		position = position < 0 ? 0 : position;
		position = position % mCount;
		T data = getItem(position);
		Object filePathObj = getFileValue(data);
		ImageView view = null;
		if(convertView == null) {
			view = new ImageView(mContext);
			view.setLayoutParams(new Gallery.LayoutParams(
					AppUtils.getUnitDipSize(mContext, 180), AppUtils.getUnitDipSize(mContext, 120)));
			view.setBackgroundResource(R.drawable.pic_frame);
		}else {
			view = (ImageView) convertView;
		}
		if(filePathObj == null || "".equals(filePathObj.toString())) {
			setViewImage(view, mResDef);
		}else {
			view.setTag(filePathObj.toString());
			setViewImage(view,filePathObj.toString());
		}
		return view;
	}
	
	public void setViewImage(final ImageView v, int res) {
		v.setImageResource(res);
	}
	
	public void setViewImage(final ImageView v, String value) {
		Drawable cacheImage = imageLoader.loadDrawable(mContext, mImgFile,
				value, new AsyncImageLoader.ImageCallback() {
					@Override
					public void imageLoaded(Drawable imageDrawable,
							String imgPath) {
						ImageView imgTag = (ImageView) mView.findViewWithTag(imgPath);
						if (imageDrawable != null && imgTag != null) {
							imgTag.setImageDrawable(imageDrawable);
						}
					}
				});
		if (cacheImage == null) {
			v.setImageResource(mResDef);
		}else {
			v.setImageDrawable(cacheImage);
		}
	}
}
